package flc.ast.dialog;

import android.content.Context;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.R;
import flc.ast.databinding.DialogRenameStyleBinding;
import flc.ast.databinding.DialogSetPasswordBinding;
import stark.common.basic.base.BaseSmartDialog;

public class SetPasswordDialog extends BaseSmartDialog<DialogSetPasswordBinding> implements View.OnClickListener {

    public SetPasswordDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_set_password;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivPwdShow.setOnClickListener(this);
        mDataBinding.ivAgainPwdShow.setOnClickListener(this);

        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        //隐藏密码
        mDataBinding.etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        mDataBinding.etAgainPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivPwdShow://密码显示
                if (mDataBinding.etPwd.getTransformationMethod() == PasswordTransformationMethod.getInstance()) {
                    mDataBinding.ivPwdShow.setImageResource(R.drawable.aayanjing);
                    //显示密码
                    mDataBinding.etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mDataBinding.ivPwdShow.setImageResource(R.drawable.aabuxianshi);
                    //隐藏密码
                    mDataBinding.etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.ivAgainPwdShow://再次输入密码显示
                if (mDataBinding.etAgainPwd.getTransformationMethod() == PasswordTransformationMethod.getInstance()) {
                    mDataBinding.ivAgainPwdShow.setImageResource(R.drawable.aayanjing);
                    //显示密码
                    mDataBinding.etAgainPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mDataBinding.ivAgainPwdShow.setImageResource(R.drawable.aabuxianshi);
                    //隐藏密码
                    mDataBinding.etAgainPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.tvCancel:
                dismiss();
                break;
            case R.id.tvConfirm:
                dismiss();
                String pwd = mDataBinding.etPwd.getText().toString().trim();
                String againPwd = mDataBinding.etAgainPwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd) || pwd.length() != 6) {
                    ToastUtils.showShort(getContext().getString(R.string.please_again_input_pwd));
                } else if (!pwd.equals(againPwd)) {
                    ToastUtils.showShort(getContext().getString(R.string.two_pwd_different));
                } else {
                    if (listener != null) {
                        listener.click(pwd);
                    }
                }
                break;
            default:
                break;
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void click(String password);
    }
}
